package cn.szabadsag.org.controller;


import cn.szabadsag.org.domain.Employee;
import cn.szabadsag.org.query.EmployeeQuery;
import cn.szabadsag.org.service.IEmployeeService;
import cn.szabadsag.utils.AjaxResult;
import cn.szabadsag.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "部门维护的接口")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 激活用户
     * @param id 待激活用户的id
     * @return
     */
    @RequestMapping("/active")
    public AjaxResult activeEmployee(Long id){
        try {
            employeeService.activeEmployee(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("网络繁忙，请稍后重试！！！");
        }
    }


    /**
     * 新增或修改
     */
    @PutMapping
    public AjaxResult saveOrUpt(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                //  新增
                employeeService.save(employee);
            }else {
                //  修改
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 查询单个
     */
    @GetMapping(value = "/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询部门的方法")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
     * 高级查询
     * @param query
     * @return
     */
    @PostMapping
    public QueryResult<Employee> queryPage(@RequestBody EmployeeQuery query){
        return  employeeService.queryPage(query);
    }

}
